﻿using System.Data;
using SAS.Core.Result;

namespace SAS.DataAccess
{
    /// <summary>
    /// 
    /// </summary>
    public class TargetEnvironmentDAO : BaseDAO
    {
        #region Contructors

        internal TargetEnvironmentDAO()
            : base(string.Empty)
        {
        }

        public TargetEnvironmentDAO(string tokenKey)
            : base(tokenKey)
        {
        }

        #endregion

        /// <summary>
        /// Save target environment to workspace
        /// </summary>
        /// <param name="pkWorkspaceId">WorkspaceId</param>
        /// <param name="targetEnvironmentName">Target environment name</param>
        /// <returns></returns>
        public SASResult<string> Save(string pkWorkspaceId, string targetEnvironmentName)
        {
            //Indentify SQL store procedure name is
            const string storeProcedureName = "[scmAdminApp].[uspWorkSpaceRec_AddTargetEnvByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@pkWorkSpace_ID", Value = pkWorkspaceId, DbType = SqlDbType.Int},
                    new Parameter {Name = "@TargetEnvName", Value = targetEnvironmentName, DbType = SqlDbType.VarChar, Size = 20}
                };

            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }

        /// <summary>
        /// Get target environment from workspace Id
        /// </summary>
        /// <param name="pkWorkspaceId">workspace Id</param>
        /// <returns></returns>
        public SASResult<string> Get(string pkWorkspaceId)
        {
            //Indentify SQL store procedure name
            const string storeProcedureName = "[scmAdminApp].[usptblWorkSpaceRec_GetTargetEnvByToken$_ID]";

            //Build SQL parameters and their values.
            Parameter[] sqlParameters = new[]
                {
                    new Parameter {Name = "@Token", Value = TokenKey},
                    new Parameter {Name = "@pkWorkSpace_ID", Value = pkWorkspaceId, DbType = SqlDbType.Int},
                    new Parameter
                        {
                            Name = "@TargetEnvName",
                            Value = string.Empty,
                            DbType = SqlDbType.VarChar,
                            Size = 20,
                            ParameterDirection = ParameterDirection.Output,
                            OutputReturnName = "TargetEnvironment"
                        }
                };

            return DB.ExecuteScalar<string>(storeProcedureName, sqlParameters);
        }
    }
}